package com.company.cloud.captcha.modules.service.impl;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.company.cloud.captcha.modules.api.model.CaptchaVO;
import com.company.cloud.captcha.modules.model.common.RepCodeEnum;
import com.company.cloud.captcha.modules.service.CaptchaService;
import com.company.cloud.captcha.modules.util.StringUtils;
import com.company.cloud.common.exception.BusinessException;

import cn.hutool.core.util.StrUtil;

/**
 * Created by raodeming on 2019/12/25.
 */
public class DefaultCaptchaServiceImpl extends AbstractCaptchaService{

    private static Logger logger = LoggerFactory.getLogger(DefaultCaptchaServiceImpl.class);

    @Override
    public String captchaType() {
        return "default";
    }

    @Override
    public void init(Properties config) {
        super.init(config);
    }

    private CaptchaService getService(String captchaType){
        return CaptchaServiceFactory.instances.get(captchaType);
    }

    @Override
    public CaptchaVO get(CaptchaVO captchaVO) {
        if (captchaVO == null) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "captchaVO"));
        }
        if (StringUtils.isEmpty(captchaVO.getCaptchaType())) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "类型"));
        }
        return getService(captchaVO.getCaptchaType()).get(captchaVO);
    }

    @Override
    public CaptchaVO check(CaptchaVO captchaVO) {
        if (captchaVO == null) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "captchaVO"));
        }
        if (StringUtils.isEmpty(captchaVO.getCaptchaType())) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "类型"));
        }
        if (StringUtils.isEmpty(captchaVO.getToken())) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "token"));
        }
        return getService(captchaVO.getCaptchaType()).check(captchaVO);
    }

    @Override
    public CaptchaVO verification(CaptchaVO captchaVO) {
        if (captchaVO == null) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "captchaVO"));
        }
        if (StringUtils.isEmpty(captchaVO.getCaptchaVerification())) {
            throw new BusinessException(
                    RepCodeEnum.NULL_ERROR.getCode(),
                    StrUtil.format(RepCodeEnum.NULL_ERROR.getMsg(), "二次校验参数"));
        }
        try {
            String codeKey = String.format(REDIS_SECOND_CAPTCHA_KEY, captchaVO.getCaptchaVerification());
            if (!CaptchaServiceFactory.getCache(cacheType).exists(codeKey)) {
                throw new BusinessException(RepCodeEnum.API_CAPTCHA_INVALID);
            }
            //二次校验取值后，即刻失效
            CaptchaServiceFactory.getCache(cacheType).delete(codeKey);
        } catch (Exception e) {
            logger.error("验证码坐标解析失败", e);
            throw new BusinessException(e.getMessage());
        }
        return captchaVO;
    }

}
